<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Bee {
	
	///simple encrypt data
	function simpleEncrypt($data){
		$CI =& get_instance();
   		$CI->load->library('Bee');
		$startlength = 5; //please change in simpleEncrypt too
		$endlength   = 7; //please change in simpleEncrypt too
		//gen fake code
		$fake1 = $CI->bee->passwdgen($startlength);
		$fake2 = $CI->bee->passwdgen($endlength);
		return $fake1.$data.$fake2;
	}
	
	//simple decrypt data
	function simpleDecrypt($data){
		$startlength = 5; //please change in simpleEncrypt too
		$endlength   = 7; //please change in simpleEncrypt too
		$data = substr($data, $startlength);
		$endlength = $endlength*(-1);
		$tmp = substr($data,$endlength);
		$data = str_replace($tmp, '', $data);
		return $data;
	}
	
	/// check เบอร์มือถือ 08xxxxxxxx
	function Check_mobilePhone($phonenum){
		return preg_match('/^08[0-9]{8}+$/',$phonenum);
	}
	
	//input dd/mm/yy
	//output yy/mm/dd
	function datetodatabase($datedb){
		$t = explode("/",$datedb);
		$tmp = $t[2]."/".$t[1]."/".$t[0];
		return $tmp;
	}
	
	//get amphur name
	function Get_amphurname($stateid,$amphurid){
		if($stateid == '0' && $amphurid){
			$s = "SELECT amphur_name FROM amphur WHERE amphur_id=".$amphurid;
			$s = mysql_query($s);
			$s = mysql_fetch_assoc($s);
			return $s['amphur_name'];
		}
		elseif($stateid != '0' && $amphurid == '0'){
			$s = "SELECT amphur_id,amphur_name FROM amphur WHERE province_id=".$stateid;
			$s = mysql_query($s);
			while($t = mysql_fetch_assoc($s)){
				$c[$t['amphur_id']] = $t['amphur_name'];
			}
			return $c;
		}
	}
	
	//check URL FORMAT
	function Check_urlformat($urlvar){
		return preg_match('/^(http|https)\:\/\/(([\d]{1,3}\.[\d]{1,3}\.[\d]{1,3}\.[\d]{1,3})|([\w\-]+\.)+(((af|ax|al|dz|as|ad|ao|ai|aq|ag|am|aw|au|at|az|bs|bh|bd|bb|by|be|bz|bj|bm|bt|bo|ba|bw|bv|br|io|bn|bg|bf|kh|cm|ca|cv|ky|cf|td|cl|cn|cx|cc|km|cg|cd|ck|cr|ci|hr|cu|cy|cz|dk|dj|dm|do|ec|eg|sv|gq|er|ee|et|fk|fo|fj|fi|fr|gf|pf|tf|ga|gm|ge|de|gh|gi|gr|gl|gd|gp|gu|gt| gg|gn|gw|gy|ht|hm|va|hn|hk|hu|is|id|ir|iq|ie|im|il|it|jm|jp|je|jo|kz|ke|ki|kp|kr|kw|kg|la|lv|lb|ls|lr|ly|li|lt|lu|mo|mk|mg|mw|my|mv|ml|mt|mh|mq|mr|yt|mx|fm|md|mc|mn|ms|ma|mz|mm|nr|np|nl|an|nc|nz|ni|ng|nu|nf|mp|no|om|pk|pw|ps|pa|pg|py|pe|ph|pn|pl|pt|qa|re|ro|ru|rw|sh|kn|lc|pm|vc|ws|sm|st|sa|sn|cs|sc|sl|sg|sk|si|sb|so|za|gs|es|lk|sd|sr|sj|sz|se|ch|sy|tw|tj|tz|th|tl|tg|tk|to|tt|tn|tr|tm|tc|tv|ug|ua|gb|us|um|uy|uz|vu|ve|vn|vg|vi|wf|eh|ye|zm|zw|uk|com|edu|gov|int|mil|net|org|biz|info|name|pro|aero|coop|museum|arpa|co|in|ne|bi|na|pr|ae|mu|ar))))(:[\d]{1,4})?($|(\/([a-zA-Z0-9\.\?=\#%&\+-])*)*|\/)$/',$urlvar);
	}
	
	//check date FORMAT dd/MM/yyy
	//Matches     29/02/2000 | 30/04/2003 | 01/01/2003
	//Non-Matches 29/02/2001 | 30-04-2003 | 1/1/2003
	function Check_dateInput($datevar){
		return preg_match('/^(((0[1-9]|[12]\d|3[01])\/(0[13578]|1[02])\/((1[6-9]|[2-9]\d)\d{2}))|((0[1-9]|[12]\d|30)\/(0[13456789]|1[012])\/((1[6-9]|[2-9]\d)\d{2}))|((0[1-9]|1\d|2[0-8])\/02\/((1[6-9]|[2-9]\d)\d{2}))|(29\/02\/((1[6-9]|[2-9]\d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00))))$/',$datevar);
	}
	
	///check
	//// กำหนดให้มีเฉพาะตัวอักษรภาษาไทยเท่านั้น มีตัวเลขที่เป็นภาษาไทยได้ แต่ห้ามมีตัวอักษรภาษาอังกฤษ
	function Check_thaistring($stringvar){
		return preg_match('/^[ก-๙]+[[:space:]]{0,1}[ก-๙]+$/',$stringvar);
	}
	//เช็ครูปแบบของเลขบัตรประชาชน
	function Check_personalid($idcode){
		if(strlen($idcode) != 13) {
			return FALSE;
		}else{
			for($i=0, $sum=0; $i<12;$i++)
				$sum += (int)($idcode{$i})*(13-$i);
			if((11-($sum%11))%10 == (int)($idcode{12})){
				return TRUE;
			}else{
				return FALSE;
			}
		}
	}
	
	// function สำหรับการแปลงข้อมูลในรูปแบบ DATE (MySQL) ให้อยู่ในรูป วันเดือนปีแบบไทย
	// return วัน/เดือน/ปี เช่น 03/05/2551
	function ToThaiTime($datetypeonly){
		$temp = explode("-",$datetypeonly);
		$year = $temp[0]+543;
		return $temp[2]."/".$temp[1]."/".$year;
	}
	
	// function สำหรับการสร้างรหัสผ่าน โดย $len คือจำนวนของรหัสผ่านที่ต้องการให้แสดง
	function passwdgen($len){
		$password = "";
		$code = "abcdefghkmnpqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ123456789";
		srand((double)microtime()*1000000);
		for($i=0;$i<$len; $i++){
			$password .= $code[rand()%strlen($code)];
		}
		return $password;
	}
		
    function Set_state($select) {
	    $c[1] = 'กระบี่';
	    $c[2] = 'กรุงเทพมหานคร';
	    $c[3] = 'กาญจนบุรี';
	    $c[4] = 'กาฬสินธุ์';
	    $c[5] = 'กำแพงเพชร';
	    $c[6] = 'ขอนแก่น';
	    $c[7] = 'จันทบุรี';
	    $c[8] = 'ฉะเชิงเทรา';
	    $c[9] = 'ชลบุรี';
	    $c[10] = 'ชัยนาท';
	    $c[11] = 'ชัยภูมิ';
	    $c[12] = 'ชุมพร';
	    $c[13] = 'เชียงราย';
	    $c[14] = 'เชียงใหม่';
	    $c[15] = 'ตรัง';
	    $c[16] = 'ตราด';
	    $c[17] = 'ตาก';
	    $c[18] = 'นครนายก';
	    $c[19] = 'นครปฐม';
	    $c[20] = 'นครพนม';
	    $c[21] = 'นครราชสีมา';
	    $c[22] = 'นครศรีธรรมราช';
	    $c[23] = 'นครสวรรค์';
	    $c[24] = 'นนทบุรี';
	    $c[25] = 'นราธิวาส';
	    $c[26] = 'น่าน';
	    $c[27] = 'บุรีรัมย์';
	    $c[28] = 'ปทุมธานี';
	    $c[29] = 'ประจวบคีรีขันธ์';
	    $c[30] = 'ปราจีนบุรี';
	    $c[31] = 'ปัตตานี';
	    $c[32] = 'พระนครศรีอยุธยา';
	    $c[33] = 'พะเยา';
	    $c[34] = 'พังงา';
	    $c[35] = 'พัทลุง';
	    $c[36] = 'พิจิตร';
	    $c[37] = 'พิษณุโลก';
	    $c[38] = 'เพชรบุรี';
	    $c[39] = ' เพชรบูรณ์';
	    $c[40] = 'แพร่';
	    $c[41] = 'ภูเก็ต';
	    $c[42] = 'มหาสารคาม';
	    $c[43] = 'มุกดาหาร';
	    $c[44] = 'แม่ฮ่องสอน';
	    $c[45] = 'ยโสธร';
	    $c[46] = 'ยะลา';
	    $c[47] = 'ร้อยเอ็ด';
	    $c[48] = 'ระนอง';
	    $c[49] = 'ระยอง';
	    $c[50] = 'ราชบุรี';
	    $c[51] = 'ลพบุรี';
	    $c[52] = 'ลำปาง';
	    $c[53] = 'ลำพูน';
	    $c[54] = 'เลย';
	    $c[55] = 'ศรีสะเกษ';
	    $c[56] = 'สกลนคร';
	    $c[57] = 'สงขลา';
	    $c[58] = 'สตูล';
	    $c[59] = 'สมุทรปราการ';
	    $c[60] = 'สมุทรสงคราม';
	    $c[61] = 'สมุทรสาคร';
	    $c[62] = 'สระแก้ว';
	    $c[63] = 'สระบุรี';
	    $c[64] = 'สิงห์บุรี';
	    $c[65] = 'สุโขทัย';
	    $c[66] = 'สุพรรณบุรี';
	    $c[67] = 'สุราษฎร์ธานี';
	    $c[68] = 'สุรินทร์';
	    $c[69] = 'หนองคาย';
	    $c[70] = 'หนองบัวลำภู';
	    $c[71] = 'อ่างทอง';
	    $c[72] = 'อำนาจเจริญ';
	    $c[73] = 'อุดรธานี';
	    $c[74] = 'อุตรดิตถ์';
	    $c[75] = 'อุทัยธานี';
	    $c[76] = 'อุบลราชธานี';
		
		if($select =='0'){
			return $c;
		}
		else{
			return $c[$select];
		}
	}
	
	function Set_country($select){
		$c['AF'] = "Afghanistan";
		$c['AL'] = "Albania";
		$c['DZ'] = "Algeria";
		$c['AS'] = "American Samoa";
		$c['AD'] = "Andorra";
		$c['AO'] = "Angola";
		$c['AI'] = "Anguilla";
		$c['AQ'] = "Antarctica";
		$c['AG'] = "Antigua/Barbuda";
		$c['AR'] = "Argentina";
		$c['AM'] = "Armenia";
		$c['AW'] = "Aruba";
		$c['AU'] = "Australia";
		$c['AT'] = "Austria";
		$c['AZ'] = "Azerbaijan";
		$c['BS'] = "Bahamas";
		$c['BH'] = "Bahrain";
		$c['BD'] = "Bangladesh";
		$c['BB'] = "Barbados";
		$c['BY'] = "Belarus";
		$c['BE'] = "Belgium";
		$c['BZ'] = "Belize";
		$c['BJ'] = "Benin";
		$c['BM'] = "Bermuda";
		$c['BT'] = "Bhutan";
		$c['IO'] = "BIOT";
		$c['BO'] = "Bolivia";
		$c['BA'] = "Bosnia/Herzegowina";
		$c['BW'] = "Botswana";
		$c['BV'] = "Bouvet Island";
		$c['BR'] = "Brazil";
		$c['BN'] = "Brunei Darussalam";
		$c['BG'] = "Bulgaria";
		$c['BF'] = "Burkina Faso";
		$c['BI'] = "Burundi";
		$c['KH'] = "Cambodia";
		$c['CM'] = "Cameroon";
		$c['CA'] = "Canada";
		$c['CV'] = "Cape Verde";
		$c['CF'] = "CAR";
		$c['KY'] = "Cayman Islands";
		$c['TD'] = "Chad";
		$c['CL'] = "Chile";
		$c['CN'] = "China";
		$c['CX'] = "Christmas Island";
		$c['CC'] = "Cocos Islands";
		$c['CO'] = "Colombia";
		$c['KM'] = "Comoros";
		$c['CG'] = "Congo";
		$c['CK'] = "Cook Islands";
		$c['CR'] = "Costa Rica";
		$c['CI'] = "Cote D'ivoire";
		$c['HR'] = "Croatia";
		$c['CU'] = "Cuba";
		$c['CY'] = "Cyprus";
		$c['CZ'] = "Czech Republic";
		$c['DK'] = "Denmark";
		$c['DJ'] = "Djibouti";
		$c['DM'] = "Dominica";
		$c['DO'] = "Dominican Republic";
		$c['TP'] = "East Timor";
		$c['EC'] = "Ecuador";
		$c['EG'] = "Egypt";
		$c['SV'] = "El Salvador";
		$c['GQ'] = "Equatorial Guinea";
		$c['ER'] = "Eritrea";
		$c['EE'] = "Estonia";
		$c['ET'] = "Ethiopia";
		$c['FK'] = "Falkland Islands";
		$c['FO'] = "Faroe Islands";
		$c['FJ'] = "Fiji";
		$c['FI'] = "Finland";
		$c['FR'] = "France";
		$c['FX'] = "France, Metropolitan";
		$c['GF'] = "French Guiana";
		$c['PF'] = "French Polynesia";
		$c['TF'] = "French So. Lands";
		$c['GA'] = "Gabon";
		$c['GM'] = "Gambia";
		$c['GZ'] = "Gaza";
		$c['GE'] = "Georgia";
		$c['DE'] = "Germany";
		$c['GH'] = "Ghana";
		$c['GI'] = "Gibraltar";
		$c['GR'] = "Greece";
		$c['GL'] = "Greenland";
		$c['GD'] = "Grenada";
		$c['GP'] = "Guadeloupe";
		$c['GU'] = "Guam";
		$c['GT'] = "Guatemala";
		$c['GN'] = "Guinea";
		$c['GW'] = "Guinea-Bissau";
		$c['GY'] = "Guyana";
		$c['HT'] = "Haiti";
		$c['HM'] = "Heard,Mc Donald Isl.";
		$c['HN'] = "Honduras";
		$c['HK'] = "Hong Kong";
		$c['HU'] = "Hungary";
		$c['IS'] = "Iceland";
		$c['IN'] = "India";
		$c['ID'] = "Indonesia";
		$c['IR'] = "Iran";
		$c['IQ'] = "Iraq";
		$c['IE'] = "Ireland";
		$c['IL'] = "Israel";
		$c['IT'] = "Italy";
		$c['JM'] = "Jamaica";
		$c['JP'] = "Japan";
		$c['JO'] = "Jordan";
		$c['KZ'] = "Kazakhstan";
		$c['KE'] = "Kenya";
		$c['KI'] = "Kiribati";
		$c['KW'] = "Kuwait";
		$c['KG'] = "Kyrgyzstan";
		$c['LA'] = "Laos";
		$c['LV'] = "Latvia";
		$c['LB'] = "Lebanon";
		$c['LS'] = "Lesotho";
		$c['LR'] = "Liberia";
		$c['LY'] = "Libya";
		$c['LI'] = "Liechtenstein";
		$c['LT'] = "Lithuania";
		$c['LU'] = "Luxembourg";
		$c['MO'] = "Macau";
		$c['MK'] = "Macedonia";
		$c['MG'] = "Madagascar";
		$c['MW'] = "Malawi";
		$c['MY'] = "Malaysia";
		$c['MV'] = "Maldives";
		$c['ML'] = "Mali";
		$c['MT'] = "Malta";
		$c['MH'] = "Marshall Islands";
		$c['MQ'] = "Martinique";
		$c['MR'] = "Mauritania";
		$c['MU'] = "Mauritius";
		$c['YT'] = "Mayotte";
		$c['MX'] = "Mexico";
		$c['FM'] = "Micronesia";
		$c['MD'] = "Moldova";
		$c['MC'] = "Monaco";
		$c['MN'] = "Mongolia";
		$c['MS'] = "Montserrat";
		$c['MA'] = "Morocco";
		$c['MZ'] = "Mozambique";
		$c['MM'] = "Myanmar";
		$c['NA'] = "Namibia";
		$c['NR'] = "Nauru";
		$c['NP'] = "Nepal";
		$c['NL'] = "Netherlands";
		$c['AN'] = "Netherlands Antilles";
		$c['NC'] = "New Caledonia";
		$c['NZ'] = "New Zealand";
		$c['NI'] = "Nicaragua";
		$c['NE'] = "Niger";
		$c['NG'] = "Nigeria";
		$c['NU'] = "Niue";
		$c['NF'] = "Norfolk Island";
		$c['KP'] = "North Korea";
		$c['MP'] = "Northern Mariana Isl.";
		$c['NO'] = "Norway";
		$c['OM'] = "Oman";
		$c['PK'] = "Pakistan";
		$c['PW'] = "Palau";
		$c['PA'] = "Panama";
		$c['PG'] = "Papua New Guinea";
		$c['PY'] = "Paraguay";
		$c['PE'] = "Peru";
		$c['PH'] = "Philippines";
		$c['PN'] = "Pitcairn";
		$c['PL'] = "Poland";
		$c['PT'] = "Portugal";
		$c['PR'] = "Puerto Rico";
		$c['QA'] = "Qatar";
		$c['RE'] = "Reunion";
		$c['RO'] = "Romania";
		$c['RU'] = "Russian Federation";
		$c['RW'] = "Rwanda";
		$c['KN'] = "Saint Kitts,Nevis";
		$c['LC'] = "Saint Lucia";
		$c['WS'] = "Samoa";
		$c['SM'] = "San Marino";
		$c['ST'] = "Sao Tome/Principe";
		$c['SA'] = "Saudi Arabia";
		$c['SN'] = "Senegal";
		$c['SC'] = "Seychelles";
		$c['SL'] = "Sierra Leone";
		$c['SG'] = "Singapore";
		$c['SK'] = "Slovak Republic";
		$c['SI'] = "Slovenia";
		$c['SB'] = "Solomon Islands";
		$c['SO'] = "Somalia";
		$c['ZA'] = "South Africa";
		$c['KR'] = "South Korea";
		$c['ES'] = "Spain";
		$c['LK'] = "Sri Lanka";
		$c['SH'] = "St. Helena";
		$c['PM'] = "St.Pierre,Miquelon";
		$c['VC'] = "St.Vincent/Grenadines";
		$c['SD'] = "Sudan";
		$c['SR'] = "Suriname";
		$c['SJ'] = "Svalbard/Jan Mayen";
		$c['SZ'] = "Swaziland";
		$c['SE'] = "Sweden";
		$c['CH'] = "Switzerland";
		$c['SY'] = "Syrian Arab Republic";
		$c['TW'] = "Taiwan";
		$c['TJ'] = "Tajikistan";
		$c['TZ'] = "Tanzania";
		$c['TH'] = "Thailand";
		$c['TG'] = "Togo";
		$c['TK'] = "Tokelau";
		$c['TO'] = "Tonga";
		$c['TT'] = "Trinidad And Tobago";
		$c['TN'] = "Tunisia";
		$c['TR'] = "Turkey";
		$c['TM'] = "Turkmenistan";
		$c['TC'] = "Turks/Caicos Isl";
		$c['TV'] = "Tuvalu";
		$c['UG'] = "Uganda";
		$c['UA'] = "Ukraine";
		$c['AE'] = "United Arab Emirates";
		$c['GB'] = "United Kingdom";
		$c['US'] = "United States";
		$c['UY'] = "Uruguay";
		$c['UZ'] = "Uzbekistan";
		$c['VU'] = "Vanuatu";
		$c['VA'] = "Vatican";
		$c['VE'] = "Venezuela";
		$c['VN'] = "Viet Nam";
		$c['VG'] = "Virgin Isl. (British)";
		$c['VI'] = "Virgin Islands (U.S.)";
		$c['WF'] = "Wallis/Futuna Isl.";
		$c['WJ'] = "West Bank";
		$c['EH'] = "Western Sahara";
		$c['YE'] = "Yemen";
		$c['YU'] = "Yugoslavia";
		$c['ZR'] = "Zaire";
		$c['ZM'] = "Zambia";
		$c['ZW'] = "Zimbabwe";
		if($select =='0'){
			return $c;
		}
		else{
			return $c[$select];
		}
	}

    ///แสดงเพศเป็นภาษาไทย
    function genderthai($gender){
    	if($gender =="F"){
    		return "หญิง";
    	}else{
    		return "ชาย";
    	}
    }
    
    /// return วันที่เป็นภาษาไทย
    // ex. 23/12/2552 17:06:26
    function thaidate($date){
    	$tmp = explode(" ",$date);
    	
    	$tmp2 = explode('-',$tmp[0]);
    	
    	$tmp2[0] = $tmp2[0] + 543;
    	
    	$newtype = $tmp2[2]."/".$tmp2[1]."/".$tmp2[0]." ".$tmp[1];
    	
    	return $newtype;
    }
    
    ///เวลาทำการของ dealer
    ///input 630
    ///output 06:30
   	function dealerworktime($openhour,$closehour){
   		
   		
   		$tmpclosehour = substr($closehour,-2,2);
   		
   		if(strlen($openhour) == '3'){
   			$hour['open'] = substr($openhour,-2,2);
   			$hour['open'] = "0".substr_replace($openhour, '', 1, 2).":".$hour['open'];
   		}else{
   			$hour['open'] = substr($openhour,-2,2);
   			$hour['open'] = substr_replace($openhour,'',2,2).":".$hour['open'];
   		}
   		if(strlen($closehour) == '4'){
   			$hour['close'] = substr($closehout,-2,2);
   			$hour['close'] = "0".substr_replace($closehour, '', 1, 2).":".$hour['close'];
   		}else{
   			$hour['close'] = substr($openhour,-2,2);
   			$hour['close'] = substr_replace($closehour,'',2,2).":".$hour['close'];
   		}
   		
   		echo $hour['open']." - ";
		echo $hour['close'];
   		
   	}
   	
   	function worktime($time){
   		if(strlen($time) == '3'){
   			$hour['minute'] = substr($time,-2,2);
   			$hour['hour'] = substr_replace($time, '', 1, 2);
   		}else{
   			$hour['minute'] = substr($time,-2,2);
   			$hour['hour'] = substr_replace($time,'',2,2);
   		}
   		return $hour;
   		
   	}
   	
   	function datepicker($date){
   		$date2 = explode("-",$date);
   		
   		$tmpdate['day'] = $date2[2];
   		$tmpdate['month'] = $date2[1];
   		$tmpdate['year'] = $date2[0];
   		
   		return $tmpdate;
   	}
 
}

?>